package internal

import (
	"github.com/name5566/leaf/gate"
	"reflect"
	"server/game"
	"server/msg"
	"github.com/name5566/leaf/log"
)

func init() {
	handleMsg(&msg.Login{}, handleAuth)
}

func handleMsg(m interface{}, h interface{}) {
	skeleton.RegisterChanRPC(reflect.TypeOf(m), h)
}

func handleAuth(args []interface{}) {
	m := args[0].(*msg.Login)
	a := args[1].(gate.Agent)

	if len(m.Account) < 2 || len(m.Account) > 12 {
		a.WriteMsg(&msg.LoginFaild{Code: msg.LoginFaild_AccIDInvalid})
		return
	}

	newAccount := creatAccountByAccountIDAndPassword(m.Account,m.Passward)
	if nil != newAccount{
		game.ChanRPC.Go("CreatePlayer", a, newAccount.PlayerID)
		game.ChanRPC.Go("UserLogin", a, newAccount.PlayerID)
	} else {
		log.Debug("create account error ",m.Account)
		a.WriteMsg(&msg.LoginFaild{Code: msg.LoginFaild_InnerError})
	}
}
